Skip to content

D3D12-LOADER-CRASH-MEMORY-AT-ISOLATION: remove post-MEM_RELEASE VA-reuse race in memory_at test - #2939

Open
bobcatchris wants to merge 1 commit into
ProjectLeonida:mainfrom
bobcatchris:fix/d3d12-crash-memory-at-race
Open

bobcatchris wants to merge 1 commit into
ProjectLeonida:mainfrom
bobcatchris:fix/d3d12-crash-memory-at-race

Conversation

@bobcatchris

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: D3D12-LOADER-CRASH-MEMORY-AT-ISOLATION (#2570 (comment); ref finding #2570 (comment))

What and why

crash::tests::memory_at_names_what_an_access_hit (d3d12/crates/leonida-loader/src/crash.rs:915) called VirtualFree(base, 0, MEM_RELEASE) and then immediately asserted assert_eq!(memory_at(base + 8), "nothing mapped there").

Under parallel leonida-loader test runs (including Windows CI), another test thread can reclaim the just-released VA range via VirtualAlloc(..., PAGE_READWRITE) or the heap allocator before VirtualQuery runs, causing intermittent failures (left: "read-write", right: "nothing mapped there").

Fix: remove the racy post-MEM_RELEASE query on base and exercise MEM_FREE against both 0 and 0x1008 in Windows' permanently unmapped low-64 KiB guard region (0..0x10000).

Tested

  • Windows 11, Rust 1.98.1, cargo test --locked -p leonida-loader --lib crash::tests: 19 passed, 0 failed.
  • python -I scripts/check_no_game_data.py d3d12/crates/leonida-loader/src/crash.rs: clean.

GPUs tested: n/a (CPU/OS virtual-memory query test; Windows 11).

AI-assisted: yes — Gemini 4 Neon (implementation and an independent read-only reviewer pass).

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.

@merzleckeier

Copy link
Copy Markdown
Contributor

Controlled address-reuse check under Wine on 561bbc19 merged into 1e3e316c. The unmodified memory_at_names_what_an_access_hit passed 3/3 on main, this PR, and this PR combined with #2946. Then I added the same test-only synchronization on all three trees. A worker in the same process reclaims the fixture’s exact released reservation before the old post-release check. In all nine probe runs the reclaimed address reported read-write. Main failed 3/3 at the old nothing mapped there assertion. This PR and the combined tree each passed 3/3, including the low-address and module checks.

With the original Cargo binary basename the stock crash tests passed 19/19 on this PR. The combined crash/profile/stack groups passed 23/23, including #2946’s closed-stderr test. This covers the test behavior under controlled reuse on Wine 11.22.

MEASURED with synthetic inputs, OpenAI Codex and a separate read-only review.

@KelboLive KelboLive left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix is right, and deletion is the only sound option: after MEM_RELEASE the test has handed the range back to the process, so any post-free VirtualQuery on base is unsound — narrowing the window would not have helped. The low-64 KiB anchor is valid (Windows keeps 0..0x10000 permanently MEM_FREE; I verified memory_at(0) and memory_at(0x1008) both return "nothing mapped there" on Win11 26200), and the PR no longer touches base after the free, so re-reservation cannot bite. crash::tests is 19/19 over 40 runs, and the old assertion is provably wrong: when I reverted it and added a throwaway free-then-VirtualAlloc-same-base probe, memory_at(base + 8) reported "read-write".

Two nits, neither blocking:

  • memory_at(0x1008) is the same MEM_FREE region as the pre-existing memory_at(0), so it adds no coverage — harmless, just not a second case.
  • The test now asserts nothing about MEM_RELEASE actually freeing the range. That is inherently untestable in-process, but worth stating explicitly rather than losing it silently.

Merge is clean against current main, and the textual overlap with #2946 is file-only (hunks are disjoint); #2947 does not touch crash.rs.

@imShangLi

Copy link
Copy Markdown
Contributor

VERIFY-2939 — Linux, Fedora 45, Rust 1.98.1, cross-compiled for x86_64-pc-windows-gnu, Wine 11.21, no GPU device, no game files

Verdict: PASS. The pull request is right about what is wrong: the assertion it removes does read the address space back as mapped by whoever else happens to allocate, and the replacements it uses read the low addresses that Windows never maps. I reproduced the race rather than trusting the comment.

Head 561bbc19, base the current main (0ae5a266), merge base ab374c7d, 1 file, +4/−1: d3d12/crates/leonida-loader/src/crash.rs.

1. What it changes

One test, crash::tests::memory_at_names_what_an_access_hit, which builds its own address space and asks [memory_at] what an access would land on. The test reserves four pages, commits three with different protections, names each one, releases them all, and then asks what the released range is:

unsafe { VirtualFree(base as *mut _, 0, MEM_RELEASE) };
-        assert_eq!(memory_at(base + 8), "nothing mapped there");
+        // Do not query `base` after `MEM_RELEASE`: concurrent test threads can
+        // immediately reuse the released VA as `PAGE_READWRITE`. Windows keeps
+        // the low 64 KiB (`0..0x10000`) permanently unmapped (`MEM_FREE`).
         assert_eq!(memory_at(0), "nothing mapped there");
+        assert_eq!(memory_at(0x1008), "nothing mapped there");
         assert_eq!(memory_at(usize::MAX), "outside the address space");

Everything is inside the mod tests block, so no production code moves: the diff is four added lines and one removed. (memory_at(0) was already on the base; the net new assertion is 0x1008, inside the low 64 KiB the comment names.)

The claim is that after MEM_RELEASE the range is back in the free pool, so a test running beside it can take those pages as PAGE_READWRITE between the VirtualFree and the read. That is exactly the window, and the comment says CI once saw read-write there.

2. The new test checks the property that matters

This is a test-only change, so the red side (merge base plus the test hunks) passes the same way the green side does, and the harness cannot show red-then-green. The thing to check instead is whether the old line really races, so I wrote a small standalone program (cross-compiled with the same toolchain and run under Wine) that does the same thing a neighbour test does: it reserves the same range with PAGE_READWRITE, commits it, spins a few thousand iterations, and releases it again, while the main thread reserves, commits, MEM_RELEASEs and reads the range back, 200 000 times.

rounds released range read back as what memory_at would say
200 000 MEM_FREE nothing mapped there — 52 156, 54 991, 58 621 over three runs
200 000 mapped in the meantime MEM_COMMIT/PAGE_READWRITE — 6 399, 5 882, 6 590
200 000 taken outright (my own VirtualAlloc failed) 141 445, 139 127, 134 789

So the old line is not deterministic: in roughly one round out of thirty the address space already holds somebody else's PAGE_READWRITE where base + 8 is, and memory_at(base + 8) reports read-write and the test fails for a reason nothing to do with the code. That is the bug the pull request names.

The two replacements sit in the low 64 KiB, which is Windows' own null-protection gap. Over the same 200 000 rounds × two addresses the racer had mapped the range thousands of times, and 0 and 0x1008 were found not-MEM_FREE 0 times. They cannot be reused because they are not offered.

I also checked the new line is live rather than decorative: substituting a value memory_at never returns made it fail with left: "nothing mapped there" / right: "a value that never happens", which also records what the real return is under Wine. Restoring the line, the test passes again.

One honest trade-off. The base used this to say something about a range it had just given back, and the pull request no longer says that about the test's own range at all — it leans on the two permanently free addresses instead. That is the right call for a test that must not be flaky, but it does drop coverage: a regression in memory_at's handling of a genuinely released range would no longer be caught by this test. Pull request #3017 keeps that property (a fresh reserve, commit, release and read, up to 64 times) while also removing the race, if the lead prefers the fix that retains coverage.

3. What the tests do here

count
red side (merge base + this pull request's test hunks) 87
green side (the pull request's head) 87
pass on both 86
red → green 0 (expected: the change touches no production code)
regressions (pass on red, fail on green) 0
fail on both 1 — loads_binds_and_calls_probe
ignored 0
only on red / only on green 0 / 0

Run by hand on the head, the crate's unit suite is 84 passed / 0 failed three times in a row, and memory_at_names_what_an_access_hit on its own is 30/30. The address space it now reads back is Windows' own, so it has nothing left that can flap.

4. Any failure I saw, and why it is not this pull request

loads_binds_and_calls_probe fails on the red side and on the green side alike, so it is not caused here. It is an integration test (d3d12/crates/leonida-loader/tests/load_probe.rs) and it dies building its own fixture:

panicked at crates/leonida-loader/tests/load_probe.rs:35:10: running cargo: Os { code: 6, kind: Uncategorized, ... }
error: manifest path `/home/shangli/.../d3d12/tests/fixtures/hle-probe/Cargo.toml` does not exist

The test shells out to cargo to compile a probe, and there is no cargo on the Wine side of this machine, and the manifest path is written with backslashes that only Windows accepts. It needs a native Windows box, not this one. Nothing else failed on either side.

5. What I did not check

  • A GPU, a D3D12 adapter, or the running title. Everything is unit tests under Wine. leonida-loader's crash module needs no device, and that is why these pass here at all.
  • Real Windows, not Wine 11.21. The claim "Windows keeps the low 64 KiB permanently unmapped" is true on Windows and true under Wine here (0 of 400 000 probes found them mapped), but the CI failure the comment refers to happened on a different machine, and I have not reproduced it there. The 64 KiB reservation is documented behaviour, so I expect it to hold; I have not proved it on the platform that reported the flake.
  • That the flake is what broke the author's build. The comment says CI once saw read-write there. I reproduced the race, not that specific run, and I cannot see their CI log.
  • The rate of contention on a loaded runner. My racer polls one address as hard as it can, which overstates it. The real neighbour is any test that calls VirtualAlloc, so the true rate is lower than the ~3 % I measured — but non-zero is enough, and I only measured the ceiling.
  • Whether the suite loses anything by dropping the released-range assertion. That is a judgement for the lead, and I have said what I think it loses above.

The program was scratch work under /tmp/opencode, cross-compiled with the same toolchain, and is not part of this pull request: nothing was committed and nothing was pushed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants